package stu.sup.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import stu.sup.dto.PicDTO;
import stu.sup.entity.Product;
import stu.sup.entity.ProductTpicInfo;
import stu.sup.mapper.ProductMapper;
import stu.sup.mapper.ProductTpicInfoMapper;
import stu.sup.service.IClassTableService;
import stu.sup.service.IProductService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HUAWEI on 2020/6/22.
 */
@Service
@Transactional
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductTpicInfoMapper productTpicInfoMapper;
    @Autowired
    private IClassTableService classTableService;

    @Override
    public int insert(Product product) {
        int row = productMapper.insert(product);
        return row;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return productMapper.deleteByPrimaryKey(id);
    }

//    @Override
//    public int updateByPrimaryKey(Product product) {
//        return productMapper.updateByPrimaryKey(product);
//    }

    @Override
    public int updateByPrimaryKeySelective(Product product) {
        return productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public List<Product> selectByConditional(Product product) {
        List<Product> productList = productMapper.findByConditional(product);
        return productList;
    }

    @Override
    public Product selectbyId(int productId) {
        return productMapper.selectByPrimaryKey(productId);
    }

    @Override
    public List<PicDTO> findAll() {
        List<Product> products=productMapper.findAll();
        List<PicDTO> picDTOS=new ArrayList<>();
        for (Product p : products) {
            ProductTpicInfo productTpicInfo=productTpicInfoMapper.selectByPrimaryKey(p.getProductImgId());
            System.out.println(productTpicInfo);
            PicDTO picDTO=new PicDTO();
            picDTO.setProductImgUrl(productTpicInfo.getPicUrl());
            picDTO.setProductName(p.getProductName());
            picDTO.setProductPrice(p.getProductPrice());
            picDTO.setProductDesc(p.getProductDesc());
            picDTO.setId(p.getId());
            picDTOS.add(picDTO);
        }
        return picDTOS;
    }

    @Override
    public List<PicDTO> findbybrand(String brand) {
        List<Product> Brands=classTableService.selectbycalssname(brand);
        List<PicDTO> picDTOS=new ArrayList<>();
        for (Product p : Brands) {
            ProductTpicInfo productTpicInfo=productTpicInfoMapper.selectByPrimaryKey(p.getProductImgId());
            PicDTO pc=new PicDTO();
            pc.setId(p.getId());
            pc.setProductName(p.getProductName());
            pc.setProductPrice(p.getProductPrice());
            pc.setProductDesc(p.getProductDesc());
            pc.setProductImgUrl(productTpicInfo.getPicUrl());

            picDTOS.add(pc);
        }
        return picDTOS;
    }


}
